﻿if (typeof Axis == "undefined") Axis = {};
if (typeof Axis.View == "undefined") Axis.View = {};
if (typeof Axis.View.Product == "undefined") Axis.View.Product = {};
Axis.View.Product.List = {
    aData: null,
    Init: function () {
        this.Event();
        this.GetTopNews();
    },
    Event: function () {
        var thisObj = this;
        $(".product_box_first_title").live('click', function () {
            ShowDetail($(this).attr('NewsID'));
        });
        $(".product_top_text").live('click', function () {
            var newsID = $(this).attr('NewsID');
            var data = null;
            var iImage = 2;
            $.each($(".product_top_list li"), function (index, item) {
                $(item).addClass('product_top_number');
                $(item).removeClass('product_top_list_active');
            });
            var tempNode = $(this).parent();
            tempNode.removeClass('product_top_number');
            tempNode.addClass('product_top_list_active');

            $.each(thisObj.aData, function (index, item) {
                if (item.ID == newsID) {
                    data = item;
                    iImage = index + 1;
                }
            });
            if (data != null)
                thisObj.ViewBoxSecond(data, iImage);
        });

    },
    GetTopNews: function () {
        var objThis = this;
        $.Ajax({
            type: "POST",
            url: '/Views/News/Service/NewsService.asmx/GetTop10',
            cache: false,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: '{' + $.string.Format('"catID":{0}', CateID) + '}',
            success: function (pData) {                
                if (pData && pData.d) {
                    if (pData.d !== null) {
                        var data = eval("(" + pData.d + ")");
                        if (data && data.length) {
                            objThis.aData = data;
                            objThis.ShowNewsList(data);
                        }
                        else {
                            $(".product_top_container").hide();
                        }
                    }
                }
                else {
                    $(".product_top_container").hide();
                }

            }
        });
    },
    ShowNewsList: function (data) {
        var thisObj = this;
        if (data.length > 0) {
            $("#Top1VoteCount").html(data[0].ProjectVoteCount);
            $("#Top1Title").html(data[0].Title);
            thisObj.ViewBoxFirst(data[0]);
            var Content = '';
            if (data.length > 1) {
                thisObj.ViewBoxSecond(data[1], 2);
                var patternActive = '<li class="product_top_list_active">'
                            + '<span class="product_top_number">2</span>'
                            + '<span class="product_top_text"  NewsID="{1}">{0}</span>'
                        + '</li>';
                var pattern = '<li class="product_top_number">'
                            + '<span class="product_top_number">{0}</span>'
                            + '<span class="product_top_text"  NewsID="{2}">{1}</span>'
                        + '</li>';
                Content += $.string.Format(patternActive, data[1].Title, data[1].ID);
                for (var i = 2; i < data.length; i++)
                    Content += $.string.Format(pattern, i + 1, data[i].Title, data[i].ID);
            }
            $('.product_top_list').html(Content);
        }

    },
    ViewBoxFirst: function (data) {
        var pattern = ' <div class="product_box_first_left">&nbsp;'
                        + ' <img class="imgFirst" src="{0}" />'
                        + ' </div>'
                        + ' <div class="product_box_first_right">'
                        + '    <div class="product_box_first_title"  NewsID="{3}">{1}</div>'
                        + '  <div class="product_border"></div>'
                        + ' <div class="product_box_first_desc">{2}</div>'
                        + ' <div class="product_border"></div>'
                        + ' <div class="top_percent">                      '
                        + ' <span class="top_percent_left"></span>         '
                        + ' <span style="width:{4}px" class="top_percent_center"></span>       '
                        + ' <span class="top_percent_right"></span>        '
                        + ' <span class="top_percent_text">{5}%</span>      '
                        + ' </div>                                         '
                        + ' </div>';
        var ProjectVotePercent = data.ProjectVotePercent * 2.5;
        //$(".product_box_first").find(".top_percent_center").width(ProjectVotePercent);
        //$(".product_box_first").find(".top_percent_text").text(data.ProjectVotePercent + "%");
        $('.product_box_first_content').html($.string.Format(pattern, data.Image, data.Title, data.Summary, data.ID, ProjectVotePercent, data.ProjectVotePercent));
        this.GetCloundTag(data.ID);
    },
    ViewBoxSecond: function (data, ZOrder) {
        var pattern = ' <div>'
                    + '&nbsp;<img class="img_top_second" src="Css/img/top_{4}.png" /><span class="imgSecondPanel"><img class="imgSecond" src="{0}" /></span><img class="img_top_second_info" src="Css/img/info.png" />'
                    + '</div>'
                    + '<div>'
                    + '<div class="product_box_first_title" NewsID="{3}">{1}</div>'
                    + '<div class="product_border"></div>'
                    + '<div class="product_box_first_desc">{2}</div>'
                    + '<div class="product_border"></div>'
                    + '</div>';
        var ProjectVotePercent = data.ProjectVotePercent * 2.5;
        $("#BlogSecond").find(".top_percent_center").width(ProjectVotePercent);
        $("#BlogSecond").find(".top_percent_text").text(data.ProjectVotePercent + "%");
        $('.BlogSecond_content').html($.string.Format(pattern, data.Image, data.Title, data.Summary, data.ID, ZOrder));
    },
    GetCloundTag: function (newsID) {
        var pData = '{' + $.string.Format('"NewsID":{0}', newsID) + '}';
        var thisObj = this;
        $.ajax({
            url: '../TagCloud/Service/ClounbTagService.asmx/GetListByNewsID',
            type: 'POST',
            data: pData,
            cache: false,
            async: false,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (pData, textStatus, jqXHR) {
                if (pData !== null)
                    if (pData.d !== null) {
                        var data = eval("(" + pData.d + ")");
                        thisObj.BuildCloundTab(data);
                    }

            },
            beforeSend: function (jqXHR, settings) {
            },
            error: function (jqXHR, textStatus, errorThrown) {
            },
            complete: function (XMLHttpRequest, textStatus) {
            }
        });

    },
    BuildCloundTab: function (data) {
        var objThis = this;
        var pattern = '<li class="tag{0}"><a href="../TagCloud/CloundTagDetail.aspx?ID={1}">{2}</a></li>';
        var htmlContent = "";
        $.each(data, function (index, item) {
            htmlContent += $.string.Format(pattern, objThis.GetRandom(1, 3), item.ID, item.Text);
        });
        $(".product_tags_content").html("<ul>" + htmlContent + "</ul>");
    },
    GetRandom: function (min, max) {
        return Math.round((Math.random() * (max - min)) + min);
    }
}
$(function () {
    Axis.View.Product.List.Init();
    $('#Project-Slider').jshowoff({
        animatePause: true,
        autoPlay: true,
        changeSpeed: 2000,
        controls: true,
        cssClass: 'thumbFeatures',
        effect: 'slideLeft',
        hoverPause: false,
        links: false,
        speed: 9000
    });
});

function ShowDetail(id) {
    window.location.assign("../News/" + id);
}